package web.util;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 对文本进行高亮处理
 */
public class HighlightToText {
    /**
     *
     * @param text 显示的文本
     * @param keywords 高亮的关键字
     * @return
     * 对文本中存在的关键字进行高亮
     */
	public static String highlight(String text,List<Map<String,Object>> keywords){
		for (Map<String,Object> keyword : keywords) {
            if (keyword.get("keyword").toString().length()==1&&macthKeyword(keyword.get("keyword").toString())){
                continue;
            }
			if (text.contains(keyword.get("keyword").toString())) {
				text=text.replace(keyword.get("keyword").toString(), "<span class=\'highlight \' style='color:#EE6363'>"+keyword.get("keyword")+"</span>");
			}
		}
		return text;
	}
        //为字符串数组添加高亮
    public static String highlight(String text,String [] keywords){
        /* 排序把关键词长的放前面 */
        String k="";
        for(int i=0;i<keywords.length;i++){
            for(int j=0;j<keywords.length;j++){
                if(keywords[i].length()>keywords[j].length()){
                    k=keywords[i];
                    keywords[i]=keywords[j];
                    keywords[j]=k;
                }
            }
        }

        for (String keyword : keywords) {
            keyword=StringOperateUtil.exChange(keyword);
            if (keyword.length()==1&&macthKeyword(keyword)){
                continue;
            }
            if (text.contains(keyword)) {
                text=text.replace(keyword, "<span class=\'highlight \' style='color:red'>"+keyword+"</span>");
            }
        }
        return text;
    }


    public static String highligthQualification(String qualification,String quali) {
        if (StringUtils.isBlank(qualification)){
            return "";
        }
        StringBuffer qualificationStringBuffer = new StringBuffer();
        String[] qualificationStrs = qualification.split(" ");
        //给包含用户设置的资质添加高亮
        for (String qualificationStr : qualificationStrs) {
            if(quali.length()>0&&quali.contains(qualificationStr)){
                qualificationStringBuffer.append("<span class=\'highlight hot\'>"+qualificationStr+"</span>,");
            }else{
                qualificationStringBuffer.append(qualificationStr+",");
            }


        }
        return qualificationStringBuffer.toString().substring(0,qualificationStringBuffer.length()-1);
    }

    /**
     *
     * @param text
     * @param keywords 资质列表
     * @return
     * 为用户关注的资质添加高亮，并且添加锚点
     */
    public static String highlightSnapQuali(String text,List<String> keywords ,String str){
        for(int i=0;i<keywords.size();i++){
            if (keywords.get(i).length()==1&&macthKeyword(keywords.get(i))){
                continue;
            }
            if(str.equals("keyword")){

                text=StringOperateUtil.exChange(text);
                String keyword=StringOperateUtil.exChange(keywords.get(i));
                if (text.contains(keyword)) {
                    if (i % 5 == 0) {
                        text = text.replace(keyword, "<a name='" + str + i+"' class='target' style='color:black'><span style='color:black;background-color:#FFBBFF;font-weight:bolder'> <span class='placeholder'></span>" +keyword + "</span></a>");
                    } else if (i % 5== 1) {
                        text = text.replace(keyword, "<a name='" + str + i+"' style='color:black'><span style='color:black;background-color:#ffff00;font-weight:bolder'>" + keyword + "</span></a>");
                    } else if (i % 5== 2) {
                        text = text.replace(keyword, "<a name='" + str + i+"' style='color:black'><span style='color:black;background-color: chartreuse;font-weight:bolder'>" + keyword + "</span></a>");
                    } else if (i % 5== 3) {
                        text = text.replace(keyword, "<a name='" + str + i+"' style='color:black'><span style='color:black;background-color: darkorange;font-weight:bolder'>" + keyword + "</span></a>");
                    } else if (i % 5== 4) {
                        text = text.replace(keyword, "<a name='" + str + i+"' style='color:black'><span style='color:black;background-color: #2abde9;font-weight:bolder'>" +keyword+ "</span></a>");
                    }

                }
            }else {
                if (text.contains(keywords.get(i))) {
                    if (i == 0) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "0' style='color:black'><span style='color:black;background-color:#FFEC8B;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    } else if (i == 1) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "1' style='color:black'><span style='color:black;background-color:#BBFFFF;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    } else if (i == 2) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "2' style='color:black'><span style='color:black;background-color: #98FB98;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    } else if (i == 3) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "3' style='color:black'><span style='color:black;background-color: #7FFF00;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                    } else if (i == 4) {
                        text = text.replace(keywords.get(i), "<a name='" + str + "4' style='color:black'><span style='color:black;background-color: #C1FFC1;font-weight:coral'>" + keywords.get(i) + "</span></a>");
                    }

                }
            }
        }
        return text;
    }

    //资质高亮
    public static List<String> findQuali(String text,List<String> keywords,String str){
        List<String>list=new ArrayList<String>();
        for(int i=0;i<keywords.size();i++){
            if (keywords.size()==1&&macthKeyword(keywords.get(i))){
                continue;
            }
            if (text.contains(keywords.get(i))) {
                if(i==0) {
//                    text = text.replace(keywords.get(i).getStr("keyword"), "<a name='keyword0' style='color:black'><span style='color:black;background-color:#FFBBFF;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                    list.add("<a href='"+str+"#quali0' style='color:black'><span style='color:black;background-color:#FFEC8B;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                }else if(i==1){
                    list.add("<a href='"+str+"#quali1' style='color:black'><span style='color:black;background-color:#BBFFFF;font-weight:bolder'>" + keywords.get(i)+ "</span></a>");
                }else if(i==2){
                    list.add("<a href='"+str+"#quali2' style='color:black'><span style='color:black;background-color: #98FB98;font-weight:bolder'>" + keywords.get(i)+ "</span></a>");
                }else if(i==3){
                    list.add("<a href='"+str+"#quali3' style='color:black'><span style='color:black;background-color: #7FFF00;font-weight:bolder'>" +  keywords.get(i) + "</span></a>");
                }else if(i==4){
                    list.add("<a href='"+str+"#quali4' style='color:black'><span style='color:black;background-color: #C1FFC1;font-weight:bolder'>" + keywords.get(i) + "</span></a>");
                }
//               list.add(keywords.get(i).getStr("keyword"));
            }
        }
        return list;
    }


    //关键字高亮
    public static List<String> findKey(String text,List<String> keywords,String str){
        List<String>list=new ArrayList<String>();
        for(int i=0;i<keywords.size();i++){
            if (keywords.size()==1&&macthKeyword(keywords.get(i))){
                continue;
            }
            text=StringOperateUtil.exChange(text);
            String keyword=StringOperateUtil.exChange(keywords.get(i));
            if (text.contains(keyword)) {
                if(i % 5==0) {
//                    text = text.replace(keywords.get(i).getStr("keyword"), "<a name='keyword0' style='color:black'><span style='color:black;background-color:#FFBBFF;font-weight:bolder'>" + keywords.get(i).getStr("keyword") + "</span></a>");
                    list.add("<a href='"+str+"#keyword"+i+"' style='color:black'><span style='color:black;background-color:#FFBBFF;font-weight:bolder'>" + keyword + "</span></a>");
                }else if(i % 5==1){
                    list.add("<a href='"+str+"#keyword"+i+"' style='color:black'><span style='color:black;background-color:#ffff00;font-weight:bolder'>" + keyword+ "</span></a>");
                }else if(i % 5==2){
                    list.add("<a href='"+str+"#keyword"+i+"' style='color:black'><span style='color:black;background-color: chartreuse;font-weight:bolder'>" +keyword+ "</span></a>");
                }else if(i % 5==3){
                    list.add("<a href='"+str+"#keyword"+i+"' style='color:black'><span style='color:black;background-color:darkorange;font-weight:bolder'>" +keyword + "</span></a>");
                }else if(i % 5==4){
                    list.add("<a href='"+str+"#keyword"+i+"' style='color:black'><span style='color:black;background-color: #2abde9;font-weight:bolder'>" +keyword + "</span></a>");
                }

            }
        }
        return list;
    }



    //招标电话高亮
    public static String findAgentPhone(String text,String agentPhone){
        text = StringOperateUtil.exChange(text);
        agentPhone=agentPhone.trim();
        if (agentPhone!=null && text.contains(agentPhone)){
            text = text.replace(agentPhone,"<span style='color:black;background-color:#ADFF2F;font-weight:bolder'>" + agentPhone + "</span>");
        }
        return text;
    }
    public static void main(String [] arg){
       System.out.print( findAgentPhone("电话：15385600919","15385600919"));
    }


    /**
     * 对关键字添加锚点链接，可跳到对应锚点
     * @param text
     * @param keywords
     * @param
     * @return
     */

	public static String highlightForList(String text,List<String> keywords){
		for (String keyword : keywords) {

            if (keyword.length()==1&&macthKeyword(keyword)){
                continue;
            }
			if (text.contains(keyword)) {
				text=text.replace(keyword, "<span class=\'highlight \'>"+keyword+"</span>");
			}
		}
		return text;
	}
	public static String highlight(String text,String keyword){
        if(keyword!=null) {
            if (text.contains(keyword)) {
                text = text.replace(keyword, "<span class=\'highlight \'>" + keyword + "</span>");
            }
        }
		return text;
	}public static String highlight1(String text,String[] keywords){
        for (String keyword : keywords) {
            if (keyword.length()==1&&macthKeyword(keyword)){
                continue;
            }
            if (keyword != null) {
                if (text.contains(keyword)) {
                    text = text.replace(keyword, "<span style='color:black;background-color:#97FFFF;font-weight:bolder' >" + keyword + "</span>");
                }
            }
        }
        return text;
    }

//判断是否为英文字母
    private static boolean macthKeyword(String keyword){
        Pattern pattern = Pattern.compile("^[a-z]$");
        return pattern.matcher(keyword).find();
    }


    /**
     * 对标题
     * @param list
     * @param key
     */
    public static void highlightToTitle(List<Map<String,Object>> list ,String key){
        for (Map<String, Object> record : list) {
//            对标题增加搞亮
            if (key.isEmpty()) {
                record.put("title_key", record.get("title"));
            } else {

                List<String> lists = StringOperateUtil.getKeyword(key.trim());

                int size =lists.size();
                String[] str = new String[size];
                for(int i=0;i<lists.size();i++){
                    str[i]=lists.get(i);
                }
                String titleStr="";
                /*if(record.get("title").toString().length()>40){
                    titleStr=record.get("title").toString().substring(0,40)+"...";
                }else{
                    titleStr=record.get("title").toString();
                }*/

                if(StringOperateUtil.get_length(record.get("title").toString())>32*2){
                    titleStr=titleLength(record.get("title").toString(),key,32);
                }else{
                    titleStr=record.get("title").toString();
                }

                record.put("title_key", HighlightToText.highlight(StringOperateUtil.exChange( titleStr), str));
            }
        }
    }

    /**
     * 一键通标题高亮方法
     * @param text
     * @param keywords
     * @return
     */
    public static String highlightYJT(String text,String [] keywords){
        /* 排序把关键词长的放前面 */
        String k="";
        for(int i=0;i<keywords.length;i++){
            for(int j=0;j<keywords.length;j++){
                if(keywords[i].length()>keywords[j].length()){
                    k=keywords[i];
                    keywords[i]=keywords[j];
                    keywords[j]=k;
                }
            }
        }

        for (String keyword : keywords) {
            keyword=StringOperateUtil.exChange(keyword);
            if (keyword.length()==1&&macthKeyword(keyword)){
                continue;
            }
            if (text.contains(keyword)) {
                text=text.replace(keyword, "<span style=\"color:red\" >"+keyword+"</span>");
            }
        }
        return text;
    }

    public static String titleLength(String title,String key,int length){
        //System.out.println("原始title：" + title);
        int sub=StringOperateUtil.get_length(title)-length*2;
        title=StringOperateUtil.delete_str_0(title,1,sub);
        //System.out.println("处理后title：" + title);
        return title+"...";
    }



}
